package com.walkthecow.justdraw;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;

public class ColourPicker extends Activity implements SeekBar.OnSeekBarChangeListener, TextWatcher, OnClickListener {

	public static final String KEY_COLOUR = "Colour";
	public static final String KEY_TITLE = "Title";
	
	public static final int COLOUR_VALUE_ALPHA = 0;
	public static final int COLOUR_VALUE_RED = 1;
	public static final int COLOUR_VALUE_GREEN = 2;
	public static final int COLOUR_VALUE_BLUE = 3;

	private SeekBar seekBarRed;
	private EditText editTextRed;
	private SeekBar seekBarGreen;
	private EditText editTextGreen;
	private SeekBar seekBarBlue;
	private EditText editTextBlue;
	private SeekBar seekBarAlpha;
	private EditText editTextAlpha;
	private ImageView colorPreview;
	private Button buttonOK;
	private Button buttonCancel;
	private int[] colourValues = new int[4];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String title = "Select Colour";

		setContentView(R.layout.colour_picker_layout);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			title = extras.getString(KEY_TITLE);
			colourValues = extras.getIntArray(KEY_COLOUR);
		}

		setTitle(title);

		seekBarRed = (SeekBar) findViewById(R.id.colour_red_seekbar);
		editTextRed = (EditText) findViewById(R.id.colour_red_value);
		seekBarGreen = (SeekBar) findViewById(R.id.colour_green_seekbar);
		editTextGreen = (EditText) findViewById(R.id.colour_green_value);
		seekBarBlue = (SeekBar) findViewById(R.id.colour_blue_seekbar);
		editTextBlue = (EditText) findViewById(R.id.colour_blue_value);
		seekBarAlpha = (SeekBar) findViewById(R.id.alpha_bar);
		editTextAlpha = (EditText) findViewById(R.id.alpha_text);
		buttonOK = (Button) findViewById(R.id.colour_button_ok);
		buttonCancel = (Button) findViewById(R.id.colour_button_cancel);

		colorPreview = (ImageView) findViewById(R.id.color_preview);

		colorPreview.setBackgroundColor(Color.argb(colourValues[COLOUR_VALUE_ALPHA], colourValues[COLOUR_VALUE_RED], colourValues[COLOUR_VALUE_GREEN], colourValues[COLOUR_VALUE_BLUE]));

		seekBarRed.setProgress(colourValues[COLOUR_VALUE_RED]);
		seekBarGreen.setProgress(colourValues[COLOUR_VALUE_GREEN]);
		seekBarBlue.setProgress(colourValues[COLOUR_VALUE_BLUE]);
		seekBarAlpha.setProgress(colourValues[COLOUR_VALUE_ALPHA]);

		editTextRed.setText(Integer.toString(colourValues[COLOUR_VALUE_RED]));
		editTextGreen.setText(Integer.toString(colourValues[COLOUR_VALUE_GREEN]));
		editTextBlue.setText(Integer.toString(colourValues[COLOUR_VALUE_BLUE]));
		editTextAlpha.setText(Integer.toString(colourValues[COLOUR_VALUE_ALPHA]));

		seekBarRed.setOnSeekBarChangeListener(this);
		seekBarGreen.setOnSeekBarChangeListener(this);
		seekBarBlue.setOnSeekBarChangeListener(this);
		seekBarAlpha.setOnSeekBarChangeListener(this);

		editTextRed.addTextChangedListener(this);
		editTextGreen.addTextChangedListener(this);
		editTextBlue.addTextChangedListener(this);
		editTextAlpha.addTextChangedListener(this);

		buttonOK.setOnClickListener(this);
		buttonCancel.setOnClickListener(this);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		
		switch (seekBar.getId()) {
		case R.id.colour_red_seekbar:
			editTextRed.setText(Integer.toString(progress));
			colourValues[COLOUR_VALUE_RED] = progress;
			break;
		case R.id.colour_green_seekbar:
			editTextGreen.setText(Integer.toString(progress));
			colourValues[COLOUR_VALUE_GREEN] = progress;
			break;
		case R.id.colour_blue_seekbar:
			editTextBlue.setText(Integer.toString(progress));
			colourValues[COLOUR_VALUE_BLUE] = progress;
			break;
		case R.id.alpha_bar:
			editTextAlpha.setText(Integer.toString(progress));
			colourValues[COLOUR_VALUE_ALPHA] = progress;
			break;
		}
		
		colorPreview.setBackgroundColor(Color.argb(seekBarAlpha.getProgress(), seekBarRed.getProgress(), seekBarGreen.getProgress(), seekBarBlue.getProgress()));

	}

	@Override
	public void onClick(View view) {
		
		Intent intent = new Intent();
		
		switch (view.getId()){
		
		case R.id.colour_button_cancel:
			setResult(Activity.RESULT_CANCELED);
			finish();
		case R.id.colour_button_ok:
			intent.putExtra(KEY_COLOUR, colourValues);
			setResult(Activity.RESULT_OK, intent);
			finish();
			break;
		}

	}

	@Override
	public void afterTextChanged(Editable s) {
		
		if(!editTextRed.getText().toString().equals("")){
			if(Integer.parseInt(editTextRed.getText().toString()) > 255){
				editTextRed.setText("255");
			}
			seekBarRed.setProgress(Integer.parseInt(editTextRed.getText().toString()));
			colourValues[COLOUR_VALUE_RED] = seekBarRed.getProgress();
		} else {
			seekBarRed.setProgress(0);
		}

		if(!editTextGreen.getText().toString().equals("")){
			if(Integer.parseInt(editTextGreen.getText().toString()) > 255){
				editTextGreen.setText("255");
			}
			seekBarGreen.setProgress(Integer.parseInt(editTextGreen.getText().toString()));
			colourValues[COLOUR_VALUE_GREEN] = seekBarGreen.getProgress();
		} else {
			seekBarGreen.setProgress(0);
		}
			
		if(!editTextBlue.getText().toString().equals("")){
			if(Integer.parseInt(editTextBlue.getText().toString()) > 255){
				editTextBlue.setText("255");
			}
			seekBarBlue.setProgress(Integer.parseInt(editTextBlue.getText().toString()));
			colourValues[COLOUR_VALUE_BLUE] = seekBarBlue.getProgress();
		}else{
			seekBarBlue.setProgress(0);
		}

		if(!editTextAlpha.getText().toString().equals("")){
			if(Integer.parseInt(editTextAlpha.getText().toString()) > 255){
				editTextAlpha.setText("255");
			}
			seekBarAlpha.setProgress(Integer.parseInt(editTextAlpha.getText().toString()));
			colourValues[COLOUR_VALUE_ALPHA] = seekBarAlpha.getProgress();
		}else{
			seekBarAlpha.setProgress(0);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		seekBar.setSecondaryProgress(seekBar.getProgress()); 
	}
}
